package com.haibei.activity.setting;

import java.io.File;
import java.util.HashMap;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.common.net.NetResult;
import com.common.task.BaseTask;
import com.common.task.NetCallBack;
import com.common.uitl.Tool;
import com.haibei.R;
import com.haibei.activity.BaseHaibeiActivity;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengDialogButtonListener;
import com.umeng.update.UmengDownloadListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

public class SettingActivity extends BaseHaibeiActivity implements OnClickListener {

	private float cacheSize = 0;
	private BaseTask baseTask = null;
	FeedbackAgent agent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		UmengUpdateAgent.update(this);//启动检查更新
		initView();
	}

	@Override
	public void initView() {
		setContentView(R.layout.activity_setting);
		findButtonById(R.id.buttonBack).setOnClickListener(this);
		findViewById(R.id.relativelayoutClearCache).setOnClickListener(this);
		findViewById(R.id.relativeLayoutChangePassword).setOnClickListener(this);
		findViewById(R.id.relativeLayoutResponseBack).setOnClickListener(this);
		findViewById(R.id.relativeLayoutVersion).setOnClickListener(listener);
		findViewById(R.id.relativeLayoutAbout).setOnClickListener(this);
		findTextViewById(R.id.textViewVersion).setText("V" + Tool.getVersionName(this));
		// if(0==cacheSize){
		computeCacheSize(SettingActivity.this.getCacheDir());
		setCacheText((int) (cacheSize / 1024 / 1024) + "M");
		// }else{
		// setCacheText((int) (cacheSize / 1024 / 1024) + "M");
		// }
		// computeCacheSizeTask();
		// relativelayoutClearCache
		agent = new FeedbackAgent(SettingActivity.this);
		agent.sync();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.buttonBack) {
			finish();
		}
		if (v.getId() == R.id.relativelayoutClearCache) {
			clearCache(getCacheDir());
			setCacheText("0M");
		}
		if (v.getId() == R.id.relativeLayoutChangePassword) {
			Tool.startActivity(this, ChangePasswordActivity.class);
		}
		if (v.getId() == R.id.relativeLayoutAbout) {
			Tool.startActivity(SettingActivity.this, AboutActivity.class);
		}
		if(v.getId()==R.id.relativeLayoutResponseBack){
			agent.startFeedbackActivity();
		}
		if(v.getId()==R.id.relativeLayoutVersion){
			
		}
	}

	/**
	 * 统计cacheSize
	 * 
	 * @param file
	 */
	private void computeCacheSize(File file) {
		if (file.isDirectory()) {
			File[] tempFs = file.listFiles();
			for (File f : tempFs) {
				computeCacheSize(f);
			}
		} else {
			cacheSize += file.length();
		}
	}

	private void setCacheText(String text) {
		if (!SettingActivity.this.isFinishing()) {
			findTextViewById(R.id.textViewCacheSize).setText(text);
		}
	}

	private void clearCache(File file) {
		if (file.isDirectory()) {
			File[] tempFs = file.listFiles();
			for (File f : tempFs) {
				f.delete();
			}
		} else {
			file.delete();
		}
	}

	private void computeCacheSizeTask() {
		if (0 == cacheSize) {
			getCacheTask().execute(new HashMap<String, String>());
		} else {
			setCacheText((int) (cacheSize / 1024 / 1024) + "M");
		}
	}

	public BaseTask getCacheTask() {
		if (null != baseTask && !baseTask.isCancelled()) {
			baseTask.cancel(true);
		}
		baseTask = new BaseTask(new NetCallBack() {
			@Override
			public void onFinish(NetResult result) {
				setCacheText((int) (cacheSize / 1024 / 1024) + "M");
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				computeCacheSize(SettingActivity.this.getCacheDir());
				return null;
			}
		});
		return baseTask;
	}
	UmengUpdateListener updateListener = new UmengUpdateListener() {
		@Override
		public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
			switch (updateStatus) {
			case 0: // has update
				Log.i("--->", "callback result");
				UmengUpdateAgent.showUpdateDialog(SettingActivity.this, updateInfo);
				break;
			case 1: // has no update
				Toast.makeText(SettingActivity.this, "没有更新", Toast.LENGTH_SHORT).show();
				break;
			case 2: // none wifi
				Toast.makeText(SettingActivity.this, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
				break;
			case 3: // time out
				Toast.makeText(SettingActivity.this, "超时", Toast.LENGTH_SHORT).show();
				break;
			case 4: // is updating
				/*
				 * Toast.makeText(mContext, "正在下载更新...", Toast.LENGTH_SHORT)
				 * .show();
				 */
				break;
			}

		}
	};
	private View.OnClickListener listener = new View.OnClickListener() {
		public void onClick(View v) {

			// 如果想程序启动时自动检查是否需要更新， 把下面两行代码加在Activity 的onCreate()函数里。
			com.umeng.common.Log.LOG = true;

			UmengUpdateAgent.setUpdateOnlyWifi(false); // 目前我们默认在Wi-Fi接入情况下才进行自动提醒。如需要在其他网络环境下进行更新自动提醒，则请添加该行代码
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateListener(updateListener);
			UmengUpdateAgent.setDownloadListener(new UmengDownloadListener() {

				@Override
				public void OnDownloadStart() {
					Toast.makeText(SettingActivity.this, "download start", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void OnDownloadUpdate(int progress) {
					Toast.makeText(SettingActivity.this, "download progress : " + progress + "%", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void OnDownloadEnd(int result, String file) {
					// Toast.makeText(mContext, "download result : " +
					// result , Toast.LENGTH_SHORT).show();
					Toast.makeText(SettingActivity.this, "download file path : " + file, Toast.LENGTH_SHORT).show();
				}
			});
			UmengUpdateAgent.setDialogListener(new UmengDialogButtonListener() {

				@Override
				public void onClick(int status) {
					switch (status) {
					case UpdateStatus.Update:
						Toast.makeText(SettingActivity.this, "User chooses update.", Toast.LENGTH_SHORT).show();
						break;
					case UpdateStatus.Ignore:
						Toast.makeText(SettingActivity.this, "User chooses ignore.", Toast.LENGTH_SHORT).show();
						break;
					case UpdateStatus.NotNow:
						Toast.makeText(SettingActivity.this, "User chooses cancel.", Toast.LENGTH_SHORT).show();
						break;
					}
				}
			});
			UmengUpdateAgent.forceUpdate(SettingActivity.this);
		}
	};
}
